<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="视频类型" prop="videoType">
        <el-select v-model="queryParams.videoType" placeholder="请选择视频类型">
          <el-option label="网格视频" value="0"/>
          <el-option label="短视频" value="1"/>
          <el-option label="漫画" value="2"/>
        </el-select>
      </el-form-item>

      <el-form-item label="分类名称" prop="categoryId" v-if="queryParams.videoType == '0'">
        <el-select v-model="queryParams.categoryId">
          <el-option
            v-for="item in categoryOptions"
            :key="item.id"
            :label="item.categoryName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="短视频用户" prop="shortVideoUserId" v-if="queryParams.videoType == '1'">
        <el-select v-model="queryParams.shortVideoUserId">
          <el-option
            v-for="item in shortUserOptions"
            :key="item.id"
            :label="item.userName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称" prop="categoryId" v-if="queryParams.videoType == '2'">
        <el-select v-model="queryParams.categoryId">
          <el-option
              v-for="item in animeCategoryOptions"
              :key="item.id"
              :label="item.categoryName"
              :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="视频标题" prop="videoName">
        <el-input
          v-model="queryParams.videoName"
          placeholder="请输入视频标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="视频标签" prop="videoLabel">
        <el-input
          v-model="queryParams.videoLabel"
          placeholder="请输入视频标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['resource:video:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['resource:video:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['resource:video:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['resource:video:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="multiple"
            @click="handleBatch"
            v-hasPermi="['resource:video:edit']"
        >批量操作</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleBatchUser"
          v-hasPermi="['resource:video:edit']"
        >批量短视频</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识" align="center" prop="id" v-if="true"/>
      <el-table-column label="视频类型" align="center" prop="videoType" >
        <template slot-scope="scope">
          {{videoTypeOptions[scope.row.videoType]['label']}}
        </template>
      </el-table-column>
      <el-table-column label="短视频用户" align="center" prop="shortVideoUserName" v-if="this.shortUserFlag"/>
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column label="视频封面" align="center" prop="videoCover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.videoCover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="视频标题" align="center" prop="videoName" />
      <el-table-column label="视频时长" align="center" prop="videoTime" />
      <el-table-column label="点赞数量" align="center" prop="likeNum" />
      <el-table-column label="收藏数量" align="center" prop="collectNum" />
      <el-table-column label="浏览数量" align="center" prop="lookNum" />
      <el-table-column label="收费类型" align="center" prop="chargeType" >
        <template slot-scope="scope">
          {{chargeTypeOptions[scope.row.chargeType]['label']}}
        </template>
      </el-table-column>
      <el-table-column label="消耗钻石" align="center" prop="consumeDiamond" />
      <el-table-column label="视频标签" align="center" prop="videoLabel" />
      <el-table-column label="是否置顶" align="center" prop="isTop" >
        <template slot-scope="scope">
          {{isTopOptions[scope.row.isTop]['label']}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['resource:video:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['resource:video:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="小说分类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择分类">
            <el-option
                v-for="item in categoryOptions"
                :key="item.id"
                :label="item.categoryName"
                :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoUrl">
          <el-input v-model="form.videoUrl" placeholder="请输入视频地址" />
        </el-form-item>
        <el-form-item label="视频封面" prop="videoCover">
          <el-input v-model="form.videoCover" placeholder="请输入视频封面"/>
        </el-form-item>
        <el-form-item label="视频标题" prop="videoName">
          <el-input v-model="form.videoName" placeholder="请输入视频标题" />
        </el-form-item>
        <el-form-item label="视频时长" prop="videoTime">
          <el-input v-model="form.videoTime" placeholder="请输入视频时长" />
        </el-form-item>
        <el-form-item label="视频标签" prop="videoLabel">
          <el-input v-model="form.videoLabel" placeholder="请输入视频标签" />
        </el-form-item>
        <el-form-item label="收费类型" prop="chargeType">
          <el-radio-group v-model="form.chargeType">
            <el-radio label="0">免费看</el-radio>
            <el-radio label="1">VIP可看</el-radio>
            <el-radio label="2">钻石观看</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="消耗钻石" prop="consumeDiamond" v-if="form.chargeType == '2'">
          <el-input v-model="form.consumeDiamond" placeholder="请输入消耗钻石" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="点赞量" prop="likeNum">
              <el-input v-model="form.likeNum" placeholder="请输入点赞量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收藏量" prop="collectNum">
              <el-input v-model="form.collectNum" placeholder="请输入收藏量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="热度量" prop="heatNum">
              <el-input v-model="form.heatNum" placeholder="请输入热度量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评论量" prop="reviewNum">
              <el-input v-model="form.reviewNum" placeholder="请输入评论量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="下载量" prop="downloadNum">
              <el-input v-model="form.downloadNum" placeholder="请输入下载量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分享量" prop="shareNum">
              <el-input v-model="form.shareNum" placeholder="请输入分享量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否置顶" prop="isTop">
          <el-radio-group v-model="form.isTop">
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="batchOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收费类型" prop="chargeType">
          <el-radio-group v-model="form.chargeType">
            <el-radio label="0">免费看</el-radio>
            <el-radio label="1">VIP可看</el-radio>
            <el-radio label="2">钻石观看</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="消耗钻石" prop="consumeDiamond" v-if="form.chargeType == '2'">
          <el-input v-model="form.consumeDiamond" placeholder="请输入消耗钻石" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitBatchForm">确 定</el-button>
        <el-button @click="batchCancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="batchUserOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="短视频用户" prop="shortVideoUserId">
          <el-select v-model="form.shortVideoUserId">
            <el-option
              v-for="item in shortUserOptions"
              :key="item.id"
              :label="item.userName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitBatchUserForm">确 定</el-button>
        <el-button @click="batchUserCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listVideo, getVideo, delVideo, addVideo, updateVideo, updateBatchVideo} from "@/api/resource/video";
import { listAllCategory } from "@/api/resource/category"
import { listAllAnimeCategory } from "@/api/pixiv/animeCategory"
import { listAllUser } from "@/api/resource/user"

export default {
  name: "Video",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      shortUserFlag: false,
      // 总条数
      total: 0,
      // 视频信息表格数据
      videoList: [],
      categoryOptions: [],
      animeCategoryOptions: [],
      shortUserOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      batchOpen: false,
      batchUserOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: undefined,
        videoType: undefined,
        videoUrl: undefined,
        videoCover: undefined,
        videoName: undefined,
        videoTime: undefined,
        likeNum: undefined,
        collectNum: undefined,
        lookNum: undefined,
        chargeType: undefined,
        consumeDiamond: undefined,
        videoLabel: undefined,
        isTop: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "唯一标识不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" }
        ],
        videoUrl: [
          { required: true, message: "视频地址不能为空", trigger: "blur" }
        ],
        videoCover: [
          { required: true, message: "视频封面不能为空", trigger: "blur" }
        ],
        videoTitle: [
          { required: true, message: "视频标题不能为空", trigger: "blur" }
        ],
        videoTime: [
          { required: true, message: "视频时长不能为空", trigger: "blur" }
        ],
        chargeType: [
          { required: true, message: "收费类型不能为空", trigger: "change" }
        ],
        videoLabel: [
          { required: true, message: "视频标签不能为空", trigger: "blur" }
        ]
      },
      videoTypeOptions: [{
        "label": "网格视频",
        "value": "0"
      }, {
        "label": "短视频",
        "value": "1"
      }, {
        "label": "动漫",
        "value": "2"
      }],
      chargeTypeOptions: [{
        "label": "免费看",
        "value": "0"
      }, {
        "label": "VIP可看",
        "value": "1"
      }, {
        "label": "钻石观看",
        "value": "2"
      }],
      isTopOptions: [{
        "label": "是",
        "value": "0"
      }, {
        "label": "否",
        "value": "1"
      }],
    };
  },
  created() {
    this.getCategory();
    this.getAnimeCategory();
    this.getShortUser();
    this.getList();
  },
  methods: {
    getCategory(){
      listAllCategory().then(res =>{
        this.categoryOptions = res.data;
      })
    },
    getAnimeCategory(){
      listAllAnimeCategory().then(res =>{
        this.animeCategoryOptions = res.data;
      })
    },
    getShortUser(){
      listAllUser().then(res =>{
        this.shortUserOptions = res.data;
      })
    },
    /** 查询视频信息列表 */
    getList() {
      this.loading = true;
      listVideo(this.queryParams).then(response => {
        this.videoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        categoryId: undefined,
        shortVideoUserId: undefined,
        videoType: undefined,
        videoUrl: undefined,
        videoCover: undefined,
        videoTitle: undefined,
        videoTime: undefined,
        likeNum: undefined,
        collectNum: undefined,
        lookNum: undefined,
        downloadNum: undefined,
        shareNum: undefined,
        chargeType: undefined,
        consumeDiamond: undefined,
        videoLabel: undefined,
        isTop: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        videoIds: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.videoType === "1"){
        this.shortUserFlag = true
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getVideo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改视频信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateVideo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            this.form.videoType = "0"
            addVideo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除视频信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delVideo(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resource/video/export', {
        ...this.queryParams
      }, `video_${new Date().getTime()}.xlsx`)
    },
    handleBatch(row) {
      const ids = row.id || this.ids;
      this.loading = true;
      this.reset();
      this.batchOpen = true;
      this.form.videoIds = ids;
      this.title = "批量配置";
    },
    // 取消按钮
    batchCancel() {
      this.batchOpen = false;
      this.reset();
    },
    submitBatchForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.videoIds != null) {
            updateBatchVideo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.batchOpen = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    handleBatchUser(row) {
      const ids = row.id || this.ids;
      this.loading = true;
      this.reset();
      this.batchUserOpen = true;
      this.form.videoIds = ids;
      this.title = "批量配置";
    },
    // 取消按钮
    batchUserCancel() {
      this.batchUserOpen = false;
      this.reset();
    },
    submitBatchUserForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.videoIds != null) {
            updateBatchVideo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.batchUserOpen = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
  }
};
</script>
